#
#  Copyright (C) 2006 by Filip Brcic <brcha@users.sourceforge.net>
#
#  This file is part of OOMTK (http://launchpad.net/oomtk)
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${sys_BINARY_DIR})

set(boot_ia32_SOURCES
      start.S
      init.cc
      CPUID.cc
      Paging.cc
      PagingLegacy.cc
      PagingPAE.cc
      GDT.cc
      IDT.cc
      InterruptHandler.cc
      InterruptManager.cc
      interrupts.S
   )


set(oomtk_LIBRARIES
    minilibc
    Devices
    supc++
    Kernel
    gcc
    glue
#     libc++
   )

add_executable(oomtk ${boot_ia32_SOURCES})
target_link_libraries(oomtk ${oomtk_LIBRARIES})
add_dependencies(oomtk ${sys_BINARY_DIR}/boot/${OOMTK_ARCH}/linker.${OOMTK_ARCH}.ld
                       OffsetGeneration IdtGeneration VersionInfo)

set_target_properties(oomtk
  PROPERTIES LINK_FLAGS "-Xlinker -T -Xlinker ${sys_BINARY_DIR}/boot/${OOMTK_ARCH}/linker.${OOMTK_ARCH}.ld"
             LINKER_LANGUAGE  CXX
  )

add_custom_command(TARGET oomtk
                   PRE_LINK
                   COMMAND ${CMAKE_C_COMPILER} -E -P -DIA32 -D__IA32__ -D__ARCH__=ia32 -I ${sys_SOURCE_DIR}/include -include ${sys_SOURCE_DIR}/include/macros.h -include ${sys_SOURCE_DIR}/include/config.h -c ${CMAKE_CURRENT_SOURCE_DIR}/linker.ia32.ld.S -o ${CMAKE_CURRENT_BINARY_DIR}/linker.ia32.ld
                  )

find_package(Grep REQUIRED)
add_custom_target(OffsetGeneration ${CMAKE_CXX_COMPILER} -S -DIA32 -D__IA32__ -D__ARCH__=ia32 -I ${sys_SOURCE_DIR}/include -I ${sys_SOURCE_DIR}/kernel -include ${sys_SOURCE_DIR}/include/macros.h -include ${sys_SOURCE_DIR}/include/config.h -o ${CMAKE_CURRENT_BINARY_DIR}/__generateOffsets__.s ${CMAKE_CURRENT_SOURCE_DIR}/__generateOffsets__.cc
                   COMMAND echo "/* THIS FILE IS AUTOGENERATED */" > ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
                   COMMAND echo \#ifndef __ARCH_OFFSETS_H__ >> ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
                   COMMAND echo \#define __ARCH_OFFSETS_H__ >> ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
                   COMMAND ${GREP} -e \"\#define\" ${CMAKE_CURRENT_BINARY_DIR}/__generateOffsets__.s | ${SED} -e 's,\\$$,,g' >> ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
                   COMMAND echo "\#endif /* __ARCH_OFFSETS_H__ */" >> ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
                 )

find_package(PythonInterp REQUIRED)
add_custom_target(IdtGeneration ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/IdtInitialization.py > ${CMAKE_CURRENT_BINARY_DIR}/__generatedIdtInitializationCode__.cc
                  COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/IdtDeclaration.py > ${CMAKE_CURRENT_BINARY_DIR}/__generatedIdtDeclarationCode__.h
		 )

# add_custom_command(OUTPUT offsets.h __generateOffsets.s
#                    COMMAND ${CMAKE_CXX_COMPILER} -S -DIA32 -D__IA32__ -D__ARCH__=ia32 -I ${SYS_SOURCE_DIR}/include -I ${SYS_SOURCE_DIR}/kernel -include ${SYS_SOURCE_DIR}/include/macros.h -include ${SYS_SOURCE_DIR}/include/config.h -o ${CMAKE_CURRENT_BINARY_DIR}/__generateOffsets.s ${CMAKE_CURRENT_SOURCE_DIR}/__generateOffsets.cpp
#                    COMMAND echo "/* THIS FILE IS AUTOGENERATED */" > ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
#                    COMMAND echo \#ifndef __ARCH_OFFSETS_H__ >> ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
#                    COMMAND echo \#define __ARCH_OFFSETS_H__ >> ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
#                    COMMAND ${GREP} \#define ${CMAKE_CURRENT_BINARY_DIR}/__generateOffsets.s | ${SED} s/\\$//g >> ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
#                    COMMAND echo "\#endif /* __ARCH_OFFSETS_H__ */" >> ${CMAKE_CURRENT_BINARY_DIR}/offsets.h
#                   )

# add_executable(oomtk ${boot_ia32_SOURCES})
# set_target_properties(oomtk PROPERTIES LINKER_LANGUAGE CXX)

# add_library(oomtkBoot STATIC ${boot_ia32_SOURCES})
# set_target_properties(oomtkBoot PROPERTIES LINKER_LANGUAGE CXX)

# Make custom commands for start.S & for linker.ia32.ld.S

# add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linker.ia32.ld
#                    COMMAND ${CMAKE_C_COMPILER} -E -P ${CMAKE_C_FLAGS} -c ${CMAKE_CURRENT_SOURCE_DIR}/linker.ia32.ld.S -o ${CMAKE_CURRENT_BINARY_DIR}/linker.ia32.ld
#                    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/linker.ia32.ld.S
#                   )

#set(CMAKE_CXX_LINK_EXECUTABLE
#  "<CMAKE_CXX_COMPILER> <FLAGS> -nostdlib -nostartfiles -nodefaultlibs -fno-rtti -fno-builtin -fno-exceptions -fomit-frame-pointer -Xlinker -T -Xlinker ${CMAKE_CURRENT_SOURCE_DIR}/linker.ld <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
#)

# add_executable(oomtk ${boot_SOURCES} ${boot_PREPROCESSED} ${boot_NASM_OBJECTS})
# target_link_libraries(oomtk ${oomtk_LIBRARIES})
#
# set_target_properties(oomtk
#   PROPERTIES LINK_FLAGS "-Xlinker -T -Xlinker ${CMAKE_CURRENT_SOURCE_DIR}/linker.ld"
#              LINKER_LANGUAGE  CXX
#   )
